﻿using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_DayJingXuan : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
            DataTable brandCategories = this.GetProductList(categoryId, 4, this.maxNum);
            if ((brandCategories != null) && (brandCategories.Rows.Count > 0))
            {
                foreach (DataRow row in brandCategories.Rows)
                {
                    builder.AppendFormat("<div class=\"pro8\">");
                    string title = string.Empty;
                    string alt = string.Empty;
                    string keyword = row["Meta_Keywords"].ToString();
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        string[] arr = keyword.Split('|');
                        if (arr.Length == 3)
                        {
                            title = arr[1];
                            alt = arr[2];
                        }
                    }
                    builder.AppendFormat("<a href=\"{0}\"><img src=\"{1}\" width=\"230px\" height=\"473px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), row["ImageUrl7"], title, alt);
                    builder.AppendFormat("</div>");
                }
            }
            return builder.ToString();
        }
        private DataTable GetProductList(int CategoryId, int TagId, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "NewID()",
                CategoryIds = CategoryId.ToString(),
                TagId = TagId,
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }

        public int maxNum { get; set; }
    }
}
